<template>

<div>
    <div style="float:left;">
      <el-button type="info" size="small" @click="dialogFormVisibleHandler">新增</el-button>
    </div>
         <el-table  :data="arr" style="width: 100%" class="biaoge">
              <el-table-column   prop="id"  label="序号"  width="180"></el-table-column>

              <el-table-column  prop="name" label="名称" width="180"></el-table-column>

              <el-table-column  prop="val" label="值">
                   <!-- <template slot-scope="scope">
                        <img width="100px" height="100px" :src=scope.row.val :alt=scope.row.val>
                   </template> -->
              </el-table-column>

              <el-table-column prop="introduce" label="介绍"></el-table-column>
                       
            
              <el-table-column  fixed="right"  label="操作"  width="270">
                
                    <template slot-scope="scope">
                        <!-- 就是根据id查看 -->
                        <!-- <el-button @click="handleClick(scope.row.id)" type="primary" size="small">查看</el-button> -->
                        <el-button type="primary" size="small" @click="toeditHandler(scope.row.id)">编辑</el-button>
                        <el-button type="danger" size="small" @click="deleteHandler(scope.row.id)">删除</el-button>
                    </template>
             </el-table-column>
          </el-table>
            
    <!-- 添加方法的弹框 -->
<el-dialog title="新增配置信息" :visible.sync="dialogFormVisible">
  <el-form :model="form">
    <el-form-item label="名称" label-width="100px" required>
      <el-input v-model="form.name" autocomplete="off"></el-input>
    </el-form-item>

     <el-form-item label="介绍" label-width="100px" required>
      <el-input v-model="form.introduce" autocomplete="off"></el-input>
    </el-form-item>

    <el-form-item label="值" label-width="100px">
      <el-input v-model="form.val" autocomplete="off"></el-input>
    </el-form-item>

  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="toAddHandler">确 定</el-button>
  </div>
</el-dialog>
 <!-- 添加弹框 -->

</div>
</template>

<script>
import axios from '../../utils/request.js'
import qs from 'querystring'
export default {
    data(){
        return{
            //存放数据
            arr:[],
            dialogFormVisible:false,
            form:{ 
              name :'',
              introduce:'',
              val:''
            }
        }
    },
    methods:{
          //存放方法
          //查询所有资讯
          findall(){
              //与后台数据交互
              axios.get("/baseConfig/findAll").then(responce=>{
                  //打印response
                  console.log(responce);
                  this.arr=responce.data
              })
          },
          //模态框
        dialogFormVisibleHandler(){
        this.dialogFormVisible = true
        },

    //删除
    deleteHandler(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          axios.get('/baseConfig/deleteById?id='+id).then(res=>{
        if(res.status === 200){
          this.findall()
          this.$message({
            type:'success',
            message:'删除成功'
          })
        }else {
          alert(res.status)
        }
      })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        }); 
    },
    //编辑方法
        toeditHandler(id){
          for(let i =0; i < this.arr.length;i++){
              if(this.arr[i].id === id){
                this.form=this.arr[i];
                break;
              }
          }
          this.dialogFormVisible = true;
  
    },

    //添加
 toAddHandler(){
      axios.post("/baseConfig/saveOrUpdate",qs.stringify(this.form))
      .then((res)=>{
        if (res.status === 200) {
          this.$message({
            type:'success',
            message:'操作成功'
          })
            this.dialogFormVisible = false;
            this.findall();
          }
      });
    },
    



  },
     

      
      created(){
          //使用方法
          this.findall()
          //this.deleteHandler(id)
      }
}


</script>

<style>

</style>